<?php
// 添加预定订单
// 配置数据库连接
// 引用数据库连接文件
require_once 'databaseConnection.php';

// 创建连接
try {
    // 可以使用 $dbConn 执行数据库操作
    $dbConn = connectToDatabase();
    $dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 开启错误报告

    // 从Ajax请求中获取预订数据
    $customerName = $_POST['customerName'];
    $customerPhone = $_POST['customerPhone'];
    $roomType = $_POST['roomType'];
    $roomCount = $_POST['roomCount'];
    $checkinDate = $_POST['checkinDate'];
    $checkoutDate = $_POST['checkoutDate'];
    $adultCount = $_POST['adultCount'];
    $childCount = $_POST['childCount'];
    $scheduledTime = $_POST['scheduledTime'];
    $roomNumber = $_POST['roomNumber'];
    $paymentMethod = $_POST['paymentMethod'];
    $bookingMethod = $_POST['bookingMethod'];
    $remarks = $_POST['remarks'];




    // 准备SQL语句
    $sql = "INSERT INTO `predetermine` 
        (`customerName`, `customerPhone`, `roomType`, `roomCount`, `checkinDate`, `checkoutDate`, `adultCount`, `childCount`, `scheduledTime`, `roomNumber`, `paymentMethod`, `bookingMethod`, `remarks`) 
        VALUES 
        (:customerName, :customerPhone, :roomType, :roomCount, :checkinDate, :checkoutDate, :adultCount, :childCount, :scheduledTime, :roomNumber, :paymentMethod, :bookingMethod, :remarks)";

    // 使用预处理语句并绑定参数
    $stmt = $dbConn->prepare($sql);
    $stmt->bindParam(':customerName', $customerName);
    $stmt->bindParam(':customerPhone', $customerPhone);
    $stmt->bindParam(':roomType', $roomType);
    $stmt->bindParam(':roomCount', $roomCount);
    $stmt->bindParam(':checkinDate', $checkinDate);
    $stmt->bindParam(':checkoutDate', $checkoutDate);
    $stmt->bindParam(':adultCount', $adultCount);
    $stmt->bindParam(':childCount', $childCount);
    $stmt->bindParam(':scheduledTime', $scheduledTime);
    $stmt->bindParam(':roomNumber', $roomNumber);
    $stmt->bindParam(':paymentMethod', $paymentMethod);
    $stmt->bindParam(':bookingMethod', $bookingMethod);
    $stmt->bindParam(':remarks', $remarks);

    // 执行预处理语句并检查是否成功
    if ($stmt->execute()) {
        echo "success";
    } else {
        echo "error: " . implode(", ", $stmt->errorInfo()); // 将错误信息数组转换为字符串输出
        echo "error: " . $stmt->errorInfo();
        echo "error: " . $sql . "<br>" . $stmt->errorCode();
    }

    // 关闭连接
    $dbConn = null;
} catch (PDOException $e) {
    die("连接失败: " . $e->getMessage());
}
